﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Models;
using DVSProject.Lib;

namespace DVSProject.Controllers
{
    public class TrainingRemarkController : Controller
    {
        //
        // GET: /TrainingRemark/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TrainingRemarkRepo _TrainingRemarkRepo = new TrainingRemarkRepo();
            IEnumerable<TrainingRemarkModels> _TrainingRemarks = _TrainingRemarkRepo.GetTrainingRemark();
            return View(_TrainingRemarks);
        }

        //
        // GET: /TrainingRemark/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TrainingRemarkRepo _TrainingRemarkRepo = new TrainingRemarkRepo();
            TrainingRemarkModels _TrainingRemark = new TrainingRemarkModels();
            _TrainingRemark = _TrainingRemarkRepo.GetTrainingRemarkById (id);
            return View(_TrainingRemark);
        }

        //
        // GET: /TrainingRemark/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TrainingRemarkModels _TrainingRemark = new TrainingRemarkModels();

            TitleOnsiteTraningRepo _TitleOnsiteTraningRepo = new TitleOnsiteTraningRepo();
            IEnumerable<TitleOnsiteTraningModels> _TitleOnsiteTranings = _TitleOnsiteTraningRepo.GetTitleOnsiteTraning();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleOnsiteTranings
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleOnsiteTraningId == selectedId),
                                                         Text = s.TitleOnsiteTraningName,
                                                         Value = s.TitleOnsiteTraningId.ToString()
                                                     };

            _TrainingRemark.TitleSelect = selectlist;
            return View(_TrainingRemark);
        } 

        //
        // POST: /TrainingRemark/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                TrainingRemarkRepo _TrainingRemarkRepo = new TrainingRemarkRepo();
                TrainingRemarkModels _TrainingRemark = new TrainingRemarkModels();
                _TrainingRemark.TitleOnsiteTraningId = (!String.IsNullOrEmpty (collection["TitleOnsiteTraningId"].ToString ()))? SF.GetInt32 (collection["TitleOnsiteTraningId"].ToString ()):0;
                _TrainingRemark.TrainingRemarkTh = collection["TrainingRemarkTh"];
                _TrainingRemark.TrainingRemarkEng = collection["TrainingRemarkEng"];
                _TrainingRemark.CreateBy = (string)Session["User"];
                bool result = _TrainingRemarkRepo.Create(_TrainingRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /TrainingRemark/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TrainingRemarkRepo _TrainingRemarkRepo = new TrainingRemarkRepo();
            TrainingRemarkModels _TrainingRemark = new TrainingRemarkModels();
            _TrainingRemark = _TrainingRemarkRepo.GetTrainingRemarkById(id);
           
            TitleOnsiteTraningRepo _TitleOnsiteTraningRepo = new TitleOnsiteTraningRepo();
            IEnumerable<TitleOnsiteTraningModels> _TitleOnsiteTranings = _TitleOnsiteTraningRepo.GetTitleOnsiteTraning();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleOnsiteTranings
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleOnsiteTraningId == selectedId),
                                                         Text = s.TitleOnsiteTraningName,
                                                         Value = s.TitleOnsiteTraningId.ToString()
                                                     };

            _TrainingRemark.TitleSelect = selectlist;
            
            return View(_TrainingRemark);
        }

        //
        // POST: /TrainingRemark/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here

                TrainingRemarkRepo _TrainingRemarkRepo = new TrainingRemarkRepo();
                TrainingRemarkModels _TrainingRemark = new TrainingRemarkModels();
                _TrainingRemark.Id = id;
                _TrainingRemark.TitleOnsiteTraningId = (!String.IsNullOrEmpty(collection["TitleOnsiteTraningId"].ToString())) ? SF.GetInt32(collection["TitleOnsiteTraningId"].ToString()) : 0;
                _TrainingRemark.TrainingRemarkTh = collection["TrainingRemarkTh"];
                _TrainingRemark.TrainingRemarkEng = collection["TrainingRemarkEng"];
                _TrainingRemark.UpdateBy = (string)Session["User"];

                bool result = _TrainingRemarkRepo.Edit(_TrainingRemark);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TrainingRemark/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TrainingRemarkRepo _TrainingRemarkRepo = new TrainingRemarkRepo();
            TrainingRemarkModels _TrainingRemark = new TrainingRemarkModels();
            _TrainingRemark = _TrainingRemarkRepo.GetTrainingRemarkById(id);
            return View(_TrainingRemark);
        }

        //
        // POST: /TrainingRemark/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                TrainingRemarkRepo _TrainingRemarkRepo = new TrainingRemarkRepo();
                TrainingRemarkModels _TrainingRemark = new TrainingRemarkModels();
                _TrainingRemark.Id = id;
                _TrainingRemark.UpdateBy = (string)Session["User"];
                bool result = _TrainingRemarkRepo.Delete(_TrainingRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
